/*
 * FileName：WxApiCtrl.java 
 * <p>
 * Copyright (c) 2017-2020, <a href="http://www.webcsn.com">hermit (1154808491@qq.com)</a>.
 * <p>
 * Licensed under the GNU General Public License, Version 3 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/gpl-3.0.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package com.smart.wxapi.ctrl;

import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.smart.primary.common.common.BaseCtrl;
import com.smart.primary.common.exception.WxErrorException;
import com.smart.primary.common.spring.JsonView;

import com.smart.primary.common.util.HttpClientUtils;
import com.smart.primary.common.util.PropertiesUtil;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


/**
 * 微信与开发者服务器交互接口
 */
@Controller
@RequestMapping("/wxapi")
public class WxApiCtrl extends BaseCtrl {
	
	private static Logger log = LogManager.getLogger(WxApiCtrl.class);


	/**
	 */
	@RequestMapping(value = "/getToken")
	@ResponseBody
	public Object jsTicket()  {
		String  appid= PropertiesUtil.getString("weixin.appid");
		String  secret= PropertiesUtil.getString("weixin.secret");
		String 	url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appid+"&secret="+secret;
		String res = HttpClientUtils.sendHttpGet(url);
		Object obj=JSON.parse(res);
		return obj;
	}

	/**
	 */
	//http://localhost:8080/task/wxapi
	@RequestMapping(value = "/geticket")
	@ResponseBody
	public Object jsTicket(String token) throws WxErrorException {
		String  appid= PropertiesUtil.getString("weixin.appid");
		String  secret= PropertiesUtil.getString("weixin.secret");
		String 	url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appid+"&secret="+secret;
		String res = HttpClientUtils.sendHttpGet(url);
		Map  obj=(Map) JSON.parse(res);

		String 	url1 = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + obj.get("access_token") +"&type=jsapi";
		String res1 = HttpClientUtils.sendHttpGet(url1);
		Object obj1=JSON.parse(res1);
		return obj1;
	}

}
